<section class="page-wrapper order-detail">

    <a href="javascript:history.back()" class="close icon-close"></a>

    <div id="order-detail-summary" class="order-g-item" style="display: none;">
        <div class="table-name">A12</div>
        <div class="message">
            <div class="line waiter-home-order-number">订单编号：<span class="text">22</span></div>
            <div class="line waiter-home-order-time">下单时间：<span class="text">2016-09-18 10:14</span>
            </div>
            <div class="line waiter-home-order-from">订单来源：<span class="text">微信(vision_小词)</span>
            </div>
        </div>
        <div class="order-op">
            <div class="price waiter-home-order-price">￥328</div>
            <div class="compound">(优惠: <span class="text">￥20</span>)</div>
            <div class="btns">
                <button class="btn white waiter-home-order-append">拒绝</button>
                <button class="btn waiter-home-order-pay">结账</button>
            </div>
        </div>
    </div>
    <div id="waiter-food-list" class="detail-list">
    </div>
    <div id="waiter-food-price-section" class="detail-price-section" style="display: none;">
        <div class="detail-price-line">
            <div class="left">小计</div>
            <div id="detail-subtotal" class="right">￥252</div>
        </div>
        <div class="detail-price-line">
            <div class="left">优惠</div>
            <div id="detail-compound" class="right">-0.0</div>
        </div>
    </div>
    <div class="detail-price-section detail-price-line" style="display: none;">
        <div class="left">总计</div>
        <div id="detail-total" class="right">￥252</div>
    </div>
    <div id="repeat-order-food-item" class="food-item" style="display: none;">
        <div class="menu-name"><span class="name"></span><span class="count"></span></div>
        <span class="unitPrice"></span>
        <span class="isDone"><i class="icon-remove"></i></span>
    </div>
    <script>
        (function () {
            "use strict";

            window.OrderState = {
                confirmed: 0, //已确认订单
                wechat_payed: 1, //已通过微信支付的订单
                unconfirmed: 2, // 为确认订单
                cart_message: 3, // 购物车信息
                scan_qr_payed: 4, // 扫描
                offline_payed: 5,
                refused: 6
            }

            var data = common.parseHash();

            var tableId = data.tableId;
            var orderId = data.orderId;
            var subOrderId = data.subOrderId;
            if(tableId){
                orderByTableId(tableId);
            }else if(orderId){
                orderByOrderId(orderId);
            }else if(subOrderId){
                showSubOrder(subOrderId);
            }

            function showSubOrder(subOrderId) {
                api.getSubOrder(subOrderId,function(data){
                    showOnlineOrder(data.order,data.menu);
                },function(text){
                    common.toast(text);
                });
            }

            function orderByOrderId(orderId) {
                $.get('/waiterOrder/api/getParentOrderById/' + orderId, function (data) {
                    if(data && data.order && data.menu)
                         showOnlineOrder(data.order,data.menu);
                });
            }

            function orderByTableId(tableId) {
                $.get('/waiterOrder/api/getCurrentTableOrder/' + tableId, function (data) {
                    if(data && data.order && data.menu)
                        showOnlineOrder(data.order,data.menu);
                });
            }

            /*
             * 显示在线订单信息
             */
            function showOnlineOrder(order,menus) {

                if(order){
                    orderId = order.orderId;
                    var time = order.createDate;
                    var price = common.fixOne(order.price);
                    var originalPrice = common.fixOne(order.originalPrice);
//                    var tId = order.tableId;
                    var tableName = order.tableName
                    var $item = $('#order-detail-summary');
                    var compound = '-￥' + common.fixOne(originalPrice - price);
                    $item.find('.waiter-home-order-price').text('￥' + price);
                    $('#detail-total').text('￥'+price);
                    $('#detail-subtotal').text('￥'+originalPrice);
                    $('#detail-compound').text(compound);
                    $item.find('.compound .text').text(compound);
                    $('.detail-price-section').show();

//                    $item.attr('detail-order-id', orderId);
//                    $item.attr('table-id', tId);
                    $item.find('.table-name').text(tableName);
                    $item.find('.waiter-home-order-number .text').text(orderId);
                    $item.find('.waiter-home-order-time .text').text(time);

                    if(order.state == OrderState.confirmed){
                        $item.find('.waiter-home-order-append').hide();
                        $item.find('.waiter-home-order-pay').on('click', function () {
                            offlinePay(orderId);
                            return false;
                        }).text('结账').show();
                    }else if(order.state == OrderState.unconfirmed){
                        $item.find('.waiter-home-order-append').on('click',function(){
                            refuse(subOrderId);
                            return false;
                        }).text('拒绝').show();
                        $item.find('.waiter-home-order-pay').on('click', function () {
                            accept(subOrderId);
                            return false;
                        }).text('接受').show();
                    }else if(order.state == OrderState.wechat_payed){
                        $item.find('.waiter-home-order-append').off('click').hide();
                        $item.find('.waiter-home-order-pay').addClass('disabled').off('click').text('已结账');
                    }

                    $item.fadeIn();
                }

                console.log("menus: "+menus);
                if(!menus || !menus.length) return;

                var $list = $('#waiter-food-list');
                $list.html('');
                $.each(menus, function (index, item) {
                    var $item = cloneOrderItem();
                    var unitPrice = item.foodDetail.unitPrice;
                    var count = item.count;
                    var price = unitPrice * count;
                    var name = item.foodDetail.name;
                    $item.attr('map-id',item.id);
                    $item.find('.menu-name .name').text(name);
                    $item.find('.unitPrice').text('￥' + unitPrice);
                    $item.find('.count').text('x' + count);
                    $item.find('.price').text(price);
                    if(item.removeFlag == 0){
                        $item.find('.icon-remove').on('click',removeOrderItem);
                    }else{
                        $item.addClass('removed');
                        $item.find('.isDone').text('已退');
                    }

                    $list.append($item);
                });
            }

            function accept(subOrderId){
                api.acceptOrder(subOrderId,function(data){
                    common.toast('成功');
                    if(window.getDiff){
                        window.getDiff();
                    }
                    history.back();
                },function(text){
                    common.toast(text);
                });
            }

            function refuse(subOrderId) {
                api.refuseOrder(subOrderId,function(data){
                    common.toast('拒绝成功');
                    if(window.getDiff){
                        window.getDiff();
                    }
                    history.back();

                },function(text){
                    common.toast(text);
                });
            }


            /**
             * 线下结账
             */
            function offlinePay(orderId){
                var $item = $('.order-g-item[order-id='+orderId+']');
                api.offlinePay(orderId,window.userId,function(data){//success
                    history.back();
                    if(window.getDiff){
                        window.getDiff();
                    }
                },function(){//error
                    //提示结账失败
                    var tableName = $item.find('.table-name').text();
                    common.toast(tableName + ' 桌结账失败，请检测网络...');
                })
            }

            function removeOrderItem(){
                var $item = $(this).parents('.food-item');
                if(confirm('确定要退订['+$item.find('.menu-name .name').text()+']?')){
                    //退订
                    var mapId = $item.attr('map-id');
                    $.getJSON('/waiterOrder/api/delteMenuItem/'+mapId,'',function(data){
                        console.log(data);

                        if(data.status == 'success'){
                            $item.addClass('removed');
                            $item.find('.isDone').text('已退');
                            var order = data.order;
                            //修改价格等
                            var price = common.fixOne(order.price);
                            var originalPrice = common.fixOne(order.originalPrice);
                            $('#order-detail-summary .waiter-home-order-price').text('￥' + price);
                            $('#order-detail-summary .compound .text').text('-￥' + common.fixOne(originalPrice - price));
                            $('#detail-total').text('￥'+price);
                            $('#detail-subtotal').text('￥'+originalPrice);
                            $('#detail-compound').text('-￥' + common.fixOne(originalPrice - price));

                        }else{
                            //退订失败
                        }

                    }).fail(function(){
                        //网络问题
                    });
                }
            }

            var $repeatOrderItem = undefined;
            function cloneOrderItem() {
                if (!$repeatOrderItem) {
                    $repeatOrderItem = $('#repeat-order-food-item');
                }
                var $item = $repeatOrderItem.clone();
                $item.attr('id', '');
                $item.show();
                return $item;
            }

        }());
    </script>


</section>
